import 'package:flutter/material.dart';
import 'package:loxen/mode/inspiration/inspiration_info_bean.dart';
import 'package:loxen/viewmode/base_view_model.dart';
import 'package:loxen/widget/empty_widget.dart';
import 'package:loxen/widget/fail_widget.dart';
import 'package:loxen/widget/loading_widget.dart';
import 'package:provider/provider.dart';
import 'package:loxen/viewmode/mine_works_viewmodel.dart';
import 'package:flutter_pagewise/flutter_pagewise.dart';
import 'package:loxen/routes/routes.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:loxen/widget/my_subassembly.dart';

class MineWorksWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<MineWorksViewModel>(
      create: (_) {
        var model = MineWorksViewModel();
        model.loadData();
        return model;
      },
      child: Scaffold(
        appBar: PreferredSize(
          preferredSize: Size(double.infinity, 95.h),
          child: Container(
            color: Theme.of(context).backgroundColor,
            child: SafeArea(
              child: Column(
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      MySubassembly.getBackIcon(context,
                          color: Theme.of(context).textTheme.subtitle2.color),
                      Expanded(
                        child: Center(
                          child: Text(
                            StringResources.wo_de_zuo_pin,
                            style: TextStyle(
                                fontSize: 32.sp,
                                fontWeight: FontWeight.w500,
                                color: Theme.of(context).textTheme.subtitle2.color),
                          ),
                        ),
                      ),
                      MySubassembly.getSearchIcon(null, text: Text('')),
                    ],
                  )
                ],
              ),
            ),
          ),
        ),
        body: Selector<MineWorksViewModel, BaseState>(
            builder: (context, state, __) {
              if (state == BaseState.LOADING) {
                return LoadingWidget();
              }
              if (state == BaseState.CONTENT) {
                PagewiseLoadController pageLoadController = PagewiseLoadController<InspirationInfo>(
                  pageSize: 5,
                  pageFuture: (page) =>
                      Provider.of<MineWorksViewModel>(context, listen: false).nextData(page),
                );
                return Container(
                    decoration: BoxDecoration(
                      border: Border(top: BorderSide(width: 1.h, color: Color(0xFFEBECF1))),
                      color: Theme.of(context).backgroundColor,
                    ),
                    child: MediaQuery.removePadding(
                      removeTop: true,
                      context: context,
                      child: RefreshIndicator(
                        onRefresh: () async {
                          Provider.of<MineWorksViewModel>(context, listen: false).refresh();
                          pageLoadController.reset();
                        },
                        child: PagewiseListView<InspirationInfo>(
                          addAutomaticKeepAlives: false,
                          loadingBuilder: (_)=>MySubassembly.getLoadingItem(),
                          noItemsFoundBuilder: (_) => MySubassembly.getNullItem(msg: '没有更多作品'),
                          itemBuilder: (context, entity, index) {
                            return _renderMyWorks(context, entity);
                          },
                          pageLoadController: pageLoadController,
                        ),
                      ),
                    ));
              }
              if (state == BaseState.EMPTY) {
                return EmptyWidget();
              }
              if (state == BaseState.FAIL) {
                return FailWidget();
              }
              return Text('');
            },
            selector: (_, model) => model.mState),
      ),
    );
  }

  Widget _renderMyWorks(BuildContext context, InspirationInfo myWorks) {
    return GestureDetector(
      onTap: () {
        myWorks.like_num = 0;
        myWorks.is_like = 0;
        myWorks.comment_num = 0;
        Navigator.of(context)
            .pushNamed(Routes.inspiration_detail, arguments: myWorks)
            .then((value) {
          Provider.of<MineWorksViewModel>(context, listen: false).loadData();
        });
      },
      child: Container(
        color: Theme.of(context).backgroundColor,
        margin: const EdgeInsets.only(bottom: 2),
        padding: const EdgeInsets.all(10),
        child: Row(
          children: <Widget>[
            ClipRRect(
                borderRadius: BorderRadius.circular(2.5),
                child: Image.network(
                  myWorks?.cover,
                  width: 170.w,
                  height: 170.w,
                  fit: BoxFit.cover,
                )),
            SizedBox(
              width: 23,
            ),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    myWorks?.title,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      color: Color(0xFF000000),
                      fontSize: 24.sp,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  Text(
                    myWorks?.brief,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                        color: Color(0xFF82868E),
                        fontSize: 20.sp,
                        fontWeight: FontWeight.w500,
                        height: 2.5.h),
                  ),
                  Container(
                    margin: const EdgeInsets.only(top: 17),
                    child: Row(
                      children: <Widget>[
                        CircleAvatar(
                          radius: 13,
                          backgroundImage: NetworkImage(myWorks.user.portrait),
                        ),
                        SizedBox(width: 5),
                        Text(
                          myWorks.user.nickname,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            color: Color(0xFF82868E),
                            fontSize: 24.sp,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
